<?php
	
function list_type(){

//PASANG JAVASCRIPT
my_set_code_js('

function confirmDelete(id){
	var t = confirm(\'Yakin akan menghapus data ?\');
	if(t){
		location.href=\'index.php?com=type_pelatihan&task=delete&id=\'+id;
	}
	return false;
}

');
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Type pelatihan'=>array('style'=>'border-bottom:2px solid;width:85%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM type_pelatihan ";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=type_pelatihan&task=edittype&id='.$ey['type_pelatihan_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['type_pelatihan_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'type'=>  $ey['type_pelatihan_label'] ,   
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);   
	return table_builder($header , $datas ,  3 , false );
}


function form_type_validate(){
	$errsubmit = false;
	$err = array();
	 
	/*CHECK CODE*/
	$nama =  trim($_POST["nama"]); 
	$data_code = my_result("SELECT type_pelatihan_id FROM type_pelatihan WHERE type_pelatihan_label='{$nama}' "); 
	$exists_code = ( $data_code == '' ) ? false : true ;  
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Nama type belum di isi";
	}
	elseif($exists_code){
		$errsubmit = true;
		$err[] = "Nama type sudah digunakan";	
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function form_type_submit($id){
	$datas = array(
		'type_pelatihan_label'=>my_type_data_str( $_POST['nama'] ) 
	);
	if($id > 0){
		return my_update_record('type_pelatihan' ,'type_pelatihan_id' , $id , $datas);
	}
	
	return my_insert_record('type_pelatihan' , $datas);
}

function form_type_create($id = 0){
	$view = form_header( "type" , "type"  );
 	$fields = my_get_data_by_id('type_pelatihan','type_pelatihan_id',$id);
	$nama = array(
				'name'=>'nama',
				'value'=>( isset($_POST['nama']) ? $_POST['nama'] : $fields['type_pelatihan_label'] ),
				'id'=>'nama',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama);
	
	$view .= form_field_display( $form_field_nama   , "Type pelatihan"   );
	
		$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}
function remove_type($id){
	$query= "DELETE FROM type_pelatihan WHERE type_pelatihan_id = {$id}";
	return my_query($query);
} 
 